<?php


namespace app\cms\community\thirdPartyApi\aliyun;


use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class aliUser
{

    public function __construct(
        protected array $config
    ){}


    /**
     * @throws ClientException
     * @throws ServerException
     */
    public function getStsToken($query = [
        'RegionId' => "cn-hangzhou",
        'RoleArn' => "",
        'RoleSessionName' => "external-username"
    ]): array
    {
        AlibabaCloud::accessKeyClient($this->config['accessKeyId'], $this->config['accessKeySecret'])
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
        $result = AlibabaCloud::rpc()
            -> product('Sts')
            -> scheme('https')
            -> version('2015-04-01')
            -> action('AssumeRole')
            -> method('POST')
            -> host('sts.aliyuncs.com')
            -> options([
                'query' => $query,
            ])
            -> request();
        return $result -> toArray();
    }
}